import datetime
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from applications.extensions import db


class User(db.Model, UserMixin):
    __tablename__ = 'user'
    user_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
    user_name = db.Column(db.String(20), comment='用户名')
    user_phonenum = db.Column(db.String(128), comment='用户密码')
    password_hash = db.Column(db.String(128), comment='哈希密码')
    user_avatar = db.Column(db.String(255), comment='头像', default="https://ts1.cn.mm.bing.net/th/id/R-C.82e75baf62e13450ea70b017e8e73c4d?rik=k06EgGY40v6FZQ&riu=http%3a%2f%2fp4.itc.cn%2fimages01%2f20210108%2f4c8ae64e62c74035956122300a021087.jpeg&ehk=EMq8o2HI2qA51ThKwVd1y8wmjYN6K5OrFxaJ3tX0wqY%3d&risl=&pid=ImgRaw&r=0")
    user_role = db.Column(db.String(255), comment='备注')
    user_create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)